package com.mj.mafeed.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.mj.mafeed.bean.ProductCategoryJSON;
import com.mj.mafeed.bean.ProductCategoryListJSON;
import com.mj.mafeed.util.DBHelper;

/**
 * 商品分类
 * @author zhaominglei
 * @date 2016-1-9
 * 
 */
public class ProductCategoryDao extends BaseDao {
	private static final String TABLE_NAME = "feed_product_category";
	
	public int getCount(Context context) {
		SQLiteDatabase db = DBHelper.getInstance(context).getReadableDatabase();
		Cursor cursor = db.rawQuery("select count(*) from "+TABLE_NAME, null);
		Long count = null;
		if (cursor.moveToFirst()) {
			count = cursor.getLong(0);
		}
		cursor.close();
		return (count != null) ? count.intValue() : 0;
	}
	
	public void save(Context context, ProductCategoryJSON productCategoryJSON) {
		SQLiteDatabase db = DBHelper.getInstance(context).getWritableDatabase();
		ContentValues contentValues = new ContentValues();
		contentValues.put("productCategoryId", productCategoryJSON.getProductCategoryId());
		contentValues.put("orders", productCategoryJSON.getOrders());
		contentValues.put("grade", productCategoryJSON.getGrade());
		contentValues.put("name", productCategoryJSON.getName());
		contentValues.put("source", productCategoryJSON.getSource());
		contentValues.put("seoDescription", productCategoryJSON.getSeoDescription());
		contentValues.put("seoKeywords", productCategoryJSON.getSeoKeywords());
		contentValues.put("seoTitle", productCategoryJSON.getSeoTitle());
		contentValues.put("treePath", productCategoryJSON.getTreePath());
		contentValues.put("parentId", productCategoryJSON.getParentId());
		contentValues.put("appImage", productCategoryJSON.getAppImage());
		db.insert(TABLE_NAME, null, contentValues);
	}
	
	public void delete(Context context, Long productCategoryId) {
		SQLiteDatabase  db = DBHelper.getInstance(context).getWritableDatabase();
		db.execSQL("delete from "+TABLE_NAME+" where productCategoryId=?", new Object[]{ productCategoryId });
	}
	
	public void deleteAll(Context context) {
		deleteAll(context, TABLE_NAME);
	}
	
	public ProductCategoryListJSON getAll(Context context) {
		ProductCategoryListJSON productCategoryListJSON = new ProductCategoryListJSON();
		SQLiteDatabase db = DBHelper.getInstance(context).getReadableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, "orders asc");
		List<ProductCategoryJSON> recordList = new ArrayList<ProductCategoryJSON>();
		while (cursor.moveToNext()) {
			ProductCategoryJSON productCategoryJSON = new ProductCategoryJSON();
			productCategoryJSON.setProductCategoryId(cursor.getLong(cursor.getColumnIndex("productCategoryId")));
			productCategoryJSON.setOrders(cursor.getInt(cursor.getColumnIndex("orders")));
			productCategoryJSON.setGrade(cursor.getInt(cursor.getColumnIndex("grade")));
			productCategoryJSON.setName(cursor.getString(cursor.getColumnIndex("name")));
			productCategoryJSON.setSource(cursor.getString(cursor.getColumnIndex("source")));
			productCategoryJSON.setSeoDescription(cursor.getString(cursor.getColumnIndex("seoDescription")));
			productCategoryJSON.setSeoKeywords(cursor.getString(cursor.getColumnIndex("seoKeywords")));
			productCategoryJSON.setSeoTitle(cursor.getString(cursor.getColumnIndex("seoTitle")));
			productCategoryJSON.setTreePath(cursor.getString(cursor.getColumnIndex("treePath")));
			productCategoryJSON.setParentId(cursor.getLong(cursor.getColumnIndex("parentId")));
			productCategoryJSON.setAppImage(cursor.getString(cursor.getColumnIndex("appImage")));
			recordList.add(productCategoryJSON);
		}
		productCategoryListJSON.setRecordList(recordList);
		cursor.close();
		return productCategoryListJSON;
	}
}
